package com.edvinli.service.impl;

import com.edvinli.entity.Clazz;
import com.edvinli.entity.ClazzQueryString;
import com.edvinli.entity.PageBean;
import com.edvinli.mapper.ClazzMapper;
import com.edvinli.service.ClazzService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;

    /**
     * 分页查询班级信息
     * @param param
     * @return
     */
    @Override
    public PageBean page(ClazzQueryString param) {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<Clazz> list = clazzMapper.listInfo(param);
        PageInfo<Clazz> pageInfo = new PageInfo<>(list);
        return new PageBean(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 新增班级
     * @param clazz
     */
    @Override
    public void save(Clazz clazz) {
        //补充基础信息
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());

        //调用mapper层的方法来新增
        clazzMapper.insert(clazz);
    }

    /**
     * 通过id来查询回显班级信息
     * @param id
     * @return
     */
    @Override
    public Clazz getById(Integer id) {
        Clazz clazz = clazzMapper.getById(id);
        return clazz;
    }

    /**
     * 更新班级信息
     * @param clazz
     */
    @Override
    public void update(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());

        clazzMapper.update(clazz);
    }

    /**
     * 删除班级
     * @param id
     */
    @Override
    public void delete(Integer id) {
        clazzMapper.delete(id);
    }

    /**
     * 查询所有班级信息
     * @return
     */
    @Override
    public List<Clazz> getAll() {

        return clazzMapper.getAll();
    }
}
